package ahblue.cn.tanzi.model.user;


import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import java.util.List;

import ahblue.cn.tanzi.cache.ACache;
import ahblue.cn.tanzi.cache.ACacheConfig;
import ahblue.cn.tanzi.data.source.ApiCallback;
import ahblue.cn.tanzi.data.source.RemoteRepository;
import ahblue.cn.tanzi.jmessage.JMessageManage;
import ahblue.cn.tanzi.util.PinkJSON;
import ahblue.cn.tanzi.util.SharePreferenceUtil;

/**
 * Created by fengdongfei on 2018/11/9.
 * 登录返回用户id，token等重要数据
 */

public class LoginResultModel {

    /**
     * token : 043f91040c6b4fc2567be8475c9747b7
     * member_id : 2
     * member_name : pingxu
     * member_mobile : 18978966561
     * invite_code : 123123
     * member_avatar :
     * has_real : 0
     * has_invited : 1
     * circle_id : ["33","3","4"]
     */

    public String token;
    public String member_id;
    public String member_name;
    public String member_mobile;
    public String invite_code;
    public String member_avatar;
    public int has_real;
    public int has_invited;
    public List<String> circle_id;

//--------------------------------------------------------------------------------------------------

    /**
     * 获取本地存储的用户基础信息
     *
     * @param context
     * @return
     */
    public static String getUserModel(Context context) {
        //ACache cache = ACache.get(context);
        SharePreferenceUtil sharePreferenceUtil= new SharePreferenceUtil(context);
        String info = sharePreferenceUtil.getString(context,ACacheConfig.USER_INFO_BASE);
        //String info = cache.getAsString(ACacheConfig.USER_INFO_BASE);
        return info;
    }

    /**
     * 保存用户基础信息
     *
     * @param context
     * @return
     */
    public static void putUserModel(Context context, String json) {
        //ACache cache = ACache.get(context);
        //cache.put(ACacheConfig.USER_INFO_BASE, json);
        SharePreferenceUtil sharePreferenceUtil= new SharePreferenceUtil(context);
         sharePreferenceUtil.putString(context,ACacheConfig.USER_INFO_BASE,json);
    }


    /**
     * 获取用户的基础信息
     *
     * @param context
     * @param mclass
     * @return
     */
    public static LoginResultModel getUserModel(Context context, Class mclass) {
        //ACache cache = ACache.get(context);
        SharePreferenceUtil sharePreferenceUtil= new SharePreferenceUtil(context);
        String info = sharePreferenceUtil.getString(context,ACacheConfig.USER_INFO_BASE);
        //String info = cache.getAsString(ACacheConfig.USER_INFO_BASE);
        return (LoginResultModel) PinkJSON.parseObject(info, mclass);
    }

    /**
     * 清楚本地存储的用户基础信息token，id等
     *
     * @param context
     */

    public static void removeUserInfoLocal(Context context) {
        ACache cache = ACache.get(context);
        cache.remove(ACacheConfig.USER_INFO_BASE);
    }

    /**
     * 检测本地是否含有用户基础信息
     *
     * @param context
     * @return
     */
    public static boolean checkUserInfoExist(Context context) {
        String info = getUserModel(context);
        if (!TextUtils.isEmpty(info)) {
            return true;
        }
        return false;
    }

    /**
     * 获取用户id
     */
    public static String getUid(Context context) {
        ACache cache = ACache.get(context);
        String info = cache.getAsString(ACacheConfig.USER_INFO_BASE);
        return ((LoginResultModel) PinkJSON.parseObject(info, LoginResultModel.class)).member_id;
    }

    public static String getMemberName(Context context) {
        ACache cache = ACache.get(context);
        String info = cache.getAsString(ACacheConfig.USER_INFO_BASE);
        return ((LoginResultModel) PinkJSON.parseObject(info, LoginResultModel.class)).member_name;
    }

    /**
     * 获取token
     *
     * @param context
     * @return
     */
    public static String getToken(Context context) {
        //ACache cache = ACache.get(context);
       // String info = cache.getAsString(ACacheConfig.USER_INFO_BASE);
        SharePreferenceUtil sharePreferenceUtil= new SharePreferenceUtil(context);
        String info = sharePreferenceUtil.getString(context,ACacheConfig.USER_INFO_BASE);
        if (null!=((LoginResultModel) PinkJSON.parseObject(info, LoginResultModel.class))){
            return ((LoginResultModel) PinkJSON.parseObject(info, LoginResultModel.class)).token;
        }
        return "";
    }




//--------------------------------------------------------------------------------------------------

    /**
     * 保存用户详情信息
     *
     * @param context
     * @param json
     */
    public static void saveUserInfo(Context context, String json) {
        //ACache cache = ACache.get(context);
        //cache.put(ACacheConfig.USER_INFO_ALL, json);
        SharePreferenceUtil sharePreferenceUtil= new SharePreferenceUtil(context);
        sharePreferenceUtil.putString(context,ACacheConfig.USER_INFO_ALL,json);
    }

    /**
     * 清除本地用户详情信息
     *
     * @param context
     */
    public static void removeAllUserInfo(Context context) {
        ACache cache = ACache.get(context);
        cache.remove(ACacheConfig.USER_INFO_ALL);
    }

    /**
     * 获取用户详情信息对象
     *
     * @param context
     * @param mclass
     * @return
     */
    public static UserModel getUserAllModel(Context context, Class mclass) {
       // ACache cache = ACache.get(context);
        //String info = cache.getAsString(ACacheConfig.USER_INFO_ALL);
        SharePreferenceUtil sharePreferenceUtil= new SharePreferenceUtil(context);
        String info = sharePreferenceUtil.getString(context,ACacheConfig.USER_INFO_ALL);
        return (UserModel) PinkJSON.parseObject(info, mclass);
    }

    public static String getUserAllModel(Context context) {
        //ACache cache = ACache.get(context);
        //String info = cache.getAsString(ACacheConfig.USER_INFO_ALL);
        SharePreferenceUtil sharePreferenceUtil= new SharePreferenceUtil(context);
        String info = sharePreferenceUtil.getString(context,ACacheConfig.USER_INFO_ALL);
        return info;
    }

//--------------------------------------------------------------------------------------------------

    /**
     * 账号密码登陆，获取用户基础信息
     *
     * @param name
     * @param pwd
     */
    public static void LoginWithPwd(final Context context, String name, String pwd, final LoginStatus loginListener) {

        RemoteRepository repo = RemoteRepository.getsInstance();
        repo.LoginWithPwd(name, pwd, new ApiCallback<LoginResultModel>() {
            @Override
            public void onSuccess(LoginResultModel data) {
                putUserModel(context, PinkJSON.toJSON(data).toString());
                if (null != loginListener) {
                    loginListener.conver(data);

                }
            }

            @Override
            public void onFailed(String errorMsg) {
                Toast.makeText(context, errorMsg, Toast.LENGTH_LONG).show();
                if (null != loginListener) {
                    loginListener.conver(null);
                }
            }
        });
    }

    /**
     * 验证码登录
     *
     * @param context
     * @param name
     * @param pwd
     * @param loginListener
     */
    public static void LoginWithCode(final Context context, String name, String pwd, final LoginStatus loginListener) {

        RemoteRepository repo = RemoteRepository.getsInstance();
        repo.LoginWithCode(name, pwd, new ApiCallback<LoginResultModel>() {
            @Override
            public void onSuccess(LoginResultModel data) {
                putUserModel(context, PinkJSON.toJSON(data).toString());
                if (null != loginListener) {
                    loginListener.conver(data);
                }
            }

            @Override
            public void onFailed(String errorMsg) {
                Toast.makeText(context, errorMsg, Toast.LENGTH_LONG).show();
                if (null != loginListener) {
                    loginListener.conver(null);
                }
            }
        });
    }



    public static void update(final Context context, String name, String pwd, final LoginStatus loginListener) {

        RemoteRepository repo = RemoteRepository.getsInstance();
        repo.update(name, pwd, new ApiCallback<LoginResultModel>() {
            @Override
            public void onSuccess(LoginResultModel data) {
                putUserModel(context, PinkJSON.toJSON(data).toString());
                if (null != loginListener) {
                    loginListener.conver(data);
                }
            }

            @Override
            public void onFailed(String errorMsg) {
                Toast.makeText(context, errorMsg, Toast.LENGTH_LONG).show();
                if (null != loginListener) {
                    loginListener.conver(null);
                }
            }
        });
    }

    /**
     * 注册账号
     * @param context
     * @param name
     * @param pwd
     * @param loginListener
     */
    public static void RegistWithCode(final Context context,String name,String pwd,String invite_code,String code, final LoginStatus loginListener) {
        RemoteRepository repo = RemoteRepository.getsInstance();
        repo.RegistWithCode(name, pwd,invite_code,code, new ApiCallback<LoginResultModel>() {
            @Override
            public void onSuccess(LoginResultModel data) {
                if (null != loginListener) {
                    loginListener.conver(data);
                }
            }

            @Override
            public void onFailed(String errorMsg) {
                Toast.makeText(context, errorMsg, Toast.LENGTH_LONG).show();
                if (null != loginListener) {
                    loginListener.conver(null);
                }
            }
        });
    }

//-------------------------------------------微信登陆-------------------------------------------------------

    public static void WxLogin(final Context context, String code, final LoginStatus loginListener) {

        RemoteRepository repo = RemoteRepository.getsInstance();
        repo.WxLogin(code, new ApiCallback<LoginResultModel>() {
            @Override
            public void onSuccess(LoginResultModel data) {
                putUserModel(context, PinkJSON.toJSON(data).toString());
                UserModel.getUserInfo(context, LoginResultModel.getToken(context), new UserModel.UserInfoLoginStatus() {
                    @Override
                    public void conver(UserModel model) {
                        if (null != loginListener) {
                            loginListener.conver(data);
                        }
                    }
                });

            }

            @Override
            public void onFailed(String errorMsg) {
                Toast.makeText(context, errorMsg, Toast.LENGTH_LONG).show();
                if (null != loginListener) {
                    loginListener.conver(null);
                }
            }
        });
    }



    public static void setInvitedCode(final Context context, String code,String token, final LoginStatus loginListener) {

        RemoteRepository repo = RemoteRepository.getsInstance();
        repo.setCode(code, token,new ApiCallback<LoginResultModel>() {
            @Override
            public void onSuccess(LoginResultModel data) {
                if (null != loginListener) {
                    loginListener.conver(data);
                }
            }

            @Override
            public void onFailed(String errorMsg) {
                Toast.makeText(context, errorMsg, Toast.LENGTH_LONG).show();
                if (null != loginListener) {
                    loginListener.conver(null);
                }
            }
        });
    }


//--------------------------------------------------------------------------------------------------

    public interface LoginStatus {
        void conver(LoginResultModel model);
    }


}
